package com.asiainfo.bms.controller;


import com.asiainfo.bms.model.Borrow;
import com.asiainfo.bms.service.BookService;
import com.asiainfo.bms.service.BorrowService;
import com.asiainfo.bms.util.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 借阅表 前端控制器
 * </p>
 *
 * @author liu.hx
 * @since 2021-07-13
 */
@Api(tags = "借阅管理")
@CrossOrigin
@RestController
@RequestMapping("/bms/borrows")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;
    @Autowired
    private BookService bookService;

    @ApiOperation("获取所有借阅")
    @GetMapping("/")
    public R queryAll() {
        List<Borrow> list = borrowService.list();
        return list != null && !list.isEmpty() ?
                R.ok("获取所有借阅成功").setData("items", list) : R.error("获取所有借阅失败");
    }

    @ApiOperation("分页获取借阅记录")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("借阅查询对象") Borrow borrow) {
        IPage<Borrow> pageParam = borrowService.pageSelect(page, limit, borrow);
        List<Borrow> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取借阅成功").setData("items", list).setData("total", total) : R.error("分页获取借阅失败");
    }

    @ApiOperation("分页获取未还图书")
    @GetMapping("/return/{page}/{limit}")
    public R queryReturnByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("借阅查询对象") Borrow borrow) {
        IPage<Borrow> pageParam = borrowService.pageSelectReturn(page, limit, borrow);
        List<Borrow> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取未还图书成功").setData("items", list).setData("total", total) : R.error("分页获取未还图书失败");
    }


    @ApiOperation("新增一个借阅(借书)")
    @PostMapping("/")
    public R insert(@ApiParam(value = "借阅对象", required = true)
                    @RequestBody Borrow borrow) {
        Integer canBorrowedNum = borrowService.borrowBook(borrow);
        if (canBorrowedNum.equals(0)) {
            return R.error("借书数量达到上限（每人 5 本）");
        }
        if (canBorrowedNum.equals(-1)) {
            return R.error("该书已借空");
        }
        return R.ok("借书成功 该员工还可借阅 " + canBorrowedNum + " 本");
    }

    @ApiOperation("修改一个借阅(还书)")
    @PutMapping("/")
    public R update(@ApiParam(value = "借阅对象", required = true)
                    @RequestBody Borrow borrow) {
        Integer canBorrowedNum = borrowService.returnBook(borrow);
        if (canBorrowedNum.equals(-1)) {
            return R.error("已归还此书");
        }
        return R.ok("还书成功 该员工还可借阅 " + canBorrowedNum + " 本");
    }

    @ApiOperation("根据ID获取一个借阅")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "借阅ID", required = true)
                       @PathVariable String id) {
        Borrow borrow = borrowService.getById(id);
        return borrow != null ?
                R.ok("获取ID=" + id + "的借阅成功").setData("item", borrow) : R.error("获取ID=" + id + "的借阅失败");
    }
}

